package com.lvmama.search.spider.proxy;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.lvmama.search.spider.config.ContentFetchConfig;
import com.lvmama.search.spider.config.FetchResult;
import com.lvmama.search.spider.worker.DefaultContentFetcher;

public class XiCiProxyProvider implements IProxyProvider {

	private static Log log = LogFactory.getLog(XiCiProxyProvider.class);
	DefaultContentFetcher fetcher = new DefaultContentFetcher();

	public List<ProxyIp> getProxyIpList() {
		String baseLink = "http://www.xicidaili.com/nn/";
		List<ProxyIp> list = new ArrayList<ProxyIp>(1000);
		for (int i = 1; i <= 2; i++) {
			try {
				ContentFetchConfig config = new ContentFetchConfig();
				String finalLink = baseLink + i;
				config.setUrl(finalLink);
				FetchResult result = fetcher.getPageContent(config);
				if (result != null && result.getStatusCode() == HttpStatus.SC_OK && !result.isError()) {
					Document doc = Jsoup.parse(result.getContent());
					Element table = doc.getElementById("ip_list");
					Elements allTr = table.getElementsByTag("tr");
					for (int j = 1; j < allTr.size(); j++) {
						Elements getAllTd = allTr.get(j).getElementsByTag("td");
						String ip = getAllTd.get(1).text();
						Integer port = Integer.parseInt(getAllTd.get(2).text());
						String scheme = getAllTd.get(5).text();
						String speed = getAllTd.get(6).getElementsByTag("div").get(0).attr("title");
						// 排查速度太慢的
						boolean speedAcceptable = Double.parseDouble(speed.replaceAll("秒", "")) < 1d;
						if (speedAcceptable) {
							list.add(new ProxyIp(ip, port, scheme));
						}
					}
				} else {
					log.error("fetch finalLink:" + finalLink + "error");
				}
			} catch (Exception e) {
				log.error("fetch error", e);
			}
		}
		return list;
	}
	
}